﻿package mode7.collections
{

	
	import com.lbox.util.events.SelectionEvent;
	import com.lbox.util.interfaces.IInteractiveSelection;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;

	/**
	 * @title 			SelectionGroup
	 * @description 	Organizes a group of IInteractiveSelections
	 * @version 		1.0
	 * @since			06/25/2010
	 * @author			James Allen | Lunchbox www.lbox.com
	 * @author 			James Allen | Mode 7 Interactive www.mode7interactive.com
	 **/
	
	public class SelectionGroup extends EventDispatcher
	{
		
		private var _selections:Array;
		private var _currentSelection:IInteractiveSelection;
		private var _previousSelection:IInteractiveSelection;
		private var _index:int;
		private var _length:int;
		private var _pages:Array;
		private var _totalPages:int;
		private var _selectionsPerPage:int;
		
		public function SelectionGroup()
		{
			_selections = [];
		}
		
		public function getPageIndexOfSelection(selection:IInteractiveSelection):int
		{
			for (var i:int = 0; i < _pages.length; i ++)
			{
				if (_pages[i].indexOf(selection) != -1)
				{
					return i;
				}
			}
			return -1;
		}
		
		public function addSelection(selection:IInteractiveSelection):void
		{
			if (_selections.indexOf(selection) == -1)
			{
				_selections.push(selection);
				selection.addEventListener(SelectionEvent.SELECT, handleSelectionSelect);
				selection.addEventListener(SelectionEvent.UNSELECT, handleSelectionUnselect);
				selection.index = _selections.indexOf(selection);
			}
			formatPages();
		}
		
		public function get selections():Array
		{
			return _selections;
		}
		
		private function formatPages():void
		{
			var page:Array = [];
			_pages = [];
			
			for (var i:int = 0; i < _selections.length; i ++)
			{
				if (page.length == _selectionsPerPage && _selectionsPerPage > 0)
				{
					_pages.push(page);
					page = [];
				}
				page.push(_selections[i]);
				
			}
			if (page.length > 0)
			{
				_pages.push(page);
			}
		}
		
		private function handleSelectionUnselect(e:SelectionEvent):void 
		{
			if (_currentSelection == IInteractiveSelection(e.target))
			{
				_index = -1;
			}
			dispatchEvent(e);
		}
		
		private function handleSelectionSelect(e:SelectionEvent):void 
		{
			trace(e.target);
			if (_currentSelection == IInteractiveSelection(e.target))
			{
				return;
			}
			if (_currentSelection != null)
			{
				_previousSelection = _currentSelection;
				_previousSelection.selected = false;
			}
			
			_currentSelection = IInteractiveSelection(e.target);
			_index = _selections.indexOf(_currentSelection);
			_currentSelection.selected = true;
			dispatchEvent(e);
			dispatchEvent(new Event(Event.CHANGE));
			
		}
		
		public function removeSelection(selection:IInteractiveSelection):void
		{
			if (_selections.indexOf(selection) != -1)
			{
				
				_selections.splice(_selections.indexOf(selection), 1);
				
			}
		}
		
			
		public function getSelectionAt(index:int):IInteractiveSelection
		{
			return(_selections[index]);
		}
		
		public function get currentSelection():IInteractiveSelection { return _currentSelection; }
		
		public function get previousSelection():IInteractiveSelection { return _previousSelection; }

		public function get index():int
		{
			return _index;
		}

		public function set index(value:int):void
		{
			
			if (value < selections.length || value == -1)
			{
				_index = value;
				if (value != -1)
				{
					IInteractiveSelection(selections[_index]).selected = true;
				}
			}
			else
			{
				throw new Error("[SelectionGroup] supplied index is out of range");
			}
		}

		public function get length():int
		{
			return selections.length;
		}


		public function get selectionsPerPage():int
		{
			return _selectionsPerPage;
		}

		public function set selectionsPerPage(value:int):void
		{
			_selectionsPerPage = value;
			formatPages();
		}

		public function get totalPages():int
		{
			return _pages.length;
		}

		public function get pages():Array
		{
			return _pages;
		}


	}
}